//
//  CleanCell.m
//  掌上宏力
//
//  Created by admin on 2017/6/6.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "CleanCell.h"
@interface CleanCell()

@property (nonatomic,weak) UILabel *titLab;

@property (nonatomic,weak) UIImageView *iconView;

@property (nonatomic,weak) UILabel *detailLab;

@end

@implementation CleanCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 13, 24, 24)];
        [self.contentView addSubview:imgV];
        self.iconView = imgV;
        
        UILabel *titLab = [Factory createLabelWithFrame:CGRectMake(imgV.right+8, 0, 100, 50)];
        titLab.font = kFont(16);
        [self.contentView addSubview:titLab];
        self.titLab = titLab;
 
        UILabel *detalLab = [Factory createLabelWithFrame:CGRectMake(kScreenW-140, 0, 100, 50)];
        detalLab.font = kFont(15);
        detalLab.text = @"正在计算";
        detalLab.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:detalLab];
        self.detailLab = detalLab;
        // 设置加载视图
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [loadingView startAnimating];
        self.accessoryView = loadingView;
        
        
        [self performSelector:@selector(calculateSize) withObject:nil afterDelay:1.0];
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        //添加监听手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearFile)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}
//读取大小
-(void)calculateSize{
    CGFloat size = [self readCacheSize];
    if (size < 9999.9/1024) {
        self.detailLab.text = [NSString stringWithFormat:@"%.fKB",size*1024];
    }else{
        self.detailLab.text = [NSString stringWithFormat:@"%.2fM",size];
    }
    
    self.accessoryView = nil;
    [SVProgressHUD dismissWithDelay:1.0];
}
-(void)setDic:(NSDictionary *)dic{
    self.iconView.image = [UIImage imageNamed:dic[@"icon"]];
    self.titLab.text = dic[@"title"];
}
//清除缓存
- (void)clearFile{
    [SVProgressHUD showWithStatus:@"清理中..."];
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
        }
    }
    
    [self performSelector:@selector(calculateSize) withObject:nil afterDelay:1.0];
    
}

//计算缓存
-( float )readCacheSize{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    return [ self folderSizeAtPath :cachePath];
}
// 遍历文件夹获得文件夹大小，返回多少 M
- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    
    return folderSize/( 1024.0 * 1024.0);
    
}
// 计算 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
    }
    return 0;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *cellId = @"cellId";
    CleanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[CleanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
